﻿using FluentParser.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FluentParser.Tests
{
    public static class TestHelper
    {
        public static void AssertChildren(this ParsingNodeResult node, params bool[] success)
        {
            Assert.AreEqual(success.Length, node.Nodes.Count, "There are {0}, expected {1}", node.Nodes.Count, success.Length);
            for (int i = 0; i < success.Length; i++)
                Assert.AreEqual(success[i], node.Nodes[i].IsSuccess, "Child {0} is {1}, expected {2}", i, node.Nodes[i].IsSuccess, success[i]);
        }

        public static void AssertLength<T>(this ParsingResult<T> result, string text)
        {
            Assert.AreEqual(text.Length, result.Length);
        }

        public static void AssertLength(this ParsingNodeResult value, string text)
        {
            Assert.AreEqual(text.Length, value.Length);
        }

        public static void AssertExploredLength(this ParsingNodeResult value, string text)
        {
            Assert.AreEqual(text.Length, value.ExploredLength);
        }

        public static ParsingResult<T> Parse<T>(this BaseExpr<T> expr, string text)
        {
            return expr.Parse(new GrammarContext(text), 0);
        }

        public static ParsingNodeResult ParseNode<T>(this BaseExpr<T> expr, string text)
        {
            ParsingNodeResult node = new ParsingNodeResult(expr, 0);
            expr.ParseNode(new GrammarContext(text), node);
            return node;
        }

        //public static string Parse(this Token token, string text)
        //{
        //    TokenValue<string> value = token.TryParse(new GrammarContext<string>(text), 0);
        //    return value.Value;
        //}
    }
}
